﻿using Amazon.EC2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sustainalytics.StorageSnapshot.AWS
{
    public class EbsSnapshot
    {
        public static async Task<bool> CreateSnapshotForVolume(string volumeId)
        {
            using (var ec2Client = new AmazonEC2Client(AwsCredentials.AwsAccessKey, AwsCredentials.AwsSecretKey))
            {
                var snapshotInProgress = await CheckIfSnapshotIsInProgress(volumeId);
                if (!snapshotInProgress)
                {
                    var response = await ec2Client.CreateSnapshotAsync(
                        new Amazon.EC2.Model.CreateSnapshotRequest
                        {
                            VolumeId = volumeId,
                            Description = string.Format("Automatic snapshot {0}.{1}.{2}", DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year)
                        },
                        System.Threading.CancellationToken.None);
                }
                return true;
            }
        }

        public static async Task<bool> CheckSnapshotForVolume(string volumeId)
        {

            var ec2Config = new AmazonEC2Config();
            ec2Config.ServiceURL = "https://ec2.eu-west-1.amazonaws.com";

            using (var ec2Client = new AmazonEC2Client(AwsCredentials.AwsAccessKey, AwsCredentials.AwsSecretKey, ec2Config))
            {
                var filters = new List<Amazon.EC2.Model.Filter>
                {
                    new Amazon.EC2.Model.Filter
                    {
                        Name = "volume-id",
                        Values =  new List<string>
                        {
                            volumeId
                        }
                    }
                };

                var response = await ec2Client.DescribeSnapshotsAsync(new Amazon.EC2.Model.DescribeSnapshotsRequest
                {
                    Filters = filters
                });

                var snapshot = response.Snapshots.OrderByDescending(x => x.StartTime).FirstOrDefault();

                if (DateTime.Now.DayOfYear == snapshot.StartTime.DayOfYear)
                    return true;
                else
                    return false;
            }
        }

        static async Task<bool> CheckIfSnapshotIsInProgress(string volumeId)
        {
            using (var ec2Client = new AmazonEC2Client(AwsCredentials.AwsAccessKey, AwsCredentials.AwsSecretKey))
            {
                var filters = new List<Amazon.EC2.Model.Filter>
                {
                    new Amazon.EC2.Model.Filter
                    {
                        Name = "volume-id",
                        Values =  new List<string>
                        {
                            volumeId
                        }
                    }
                };

                var response = await ec2Client.DescribeSnapshotsAsync(new Amazon.EC2.Model.DescribeSnapshotsRequest
                {
                    Filters = filters
                });

                Console.WriteLine(response.Snapshots.Where(x => x.State == SnapshotState.Pending).FirstOrDefault().Progress);
                return response.Snapshots.Where(x => x.State == SnapshotState.Pending).Count() >= 1;
            }
        }
    }
}